﻿using System;
using System.Windows;
using System.Windows.Controls;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI.Controls;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls.GridView;
using Baosight.ZNH.ZMCL.UI.Blocks;
using Baosight.Platform.Core.ServiceModel;
using System.Collections.Generic;
using Baosight.ZNH.ZMCL.UI.PopWindows;

namespace Baosight.ZNH.ZMCL.UI
{
    public partial class ZMCL72 : EFViewCmes
    {
        private RiskBlock block = new RiskBlock();
        private string fdRecordId = "";
        public ZMCL72()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 列表中修改、删除前设置属性、参数
        /// 传递到后台代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.Grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        public override void Initialize(Baosight.Platform.Core.EIInfo initData)
        {
            initData.AddBlock(block.GetIsPenaltyBlock(true));
            initData.AddBlock(block.GetHandleStatusBlock(true));
            this.fdEndDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
            base.Initialize(initData);
        }

        /// <summary>
        /// 点击所选择的列表发生改变时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Grid_SelectionChanged(object sender, SelectionChangeEventArgs e)
        {
            if (null != this.Grid.SelectedItem)
            {
                EIRow _row = (this.Grid.SelectedItem as UIBaseEntity).GetEIRow();
                string fdStatus = _row["fdStatus"] == null ? "" : _row["fdStatus"].ToString();

                if ("关闭".Equals(fdStatus))
                {
                    Dictionary<string, string> args = new Dictionary<string, string>();
                    args.Add("fdRiskRecordId", _row["fdKeyId"].ToString());
                    this.riskDetails.LoadView<EFCustomViewContainerPanel>("ZMCL71", args);//加载从表页面

                    Dictionary<string, string> args1 = new Dictionary<string, string>();
                    args1.Add("fdPenaltyIds", _row["fdPenaltyIds"] == null ? "" : _row["fdPenaltyIds"].ToString());
                    this.riskPenalty.LoadView<EFCustomViewContainerPanel>("ZMCL75", args1);//加载从表页面
                }
                else
                {
                    this.riskDetails.Content = null;
                    this.riskPenalty.Content = null;
                }
            }
            else
            {
                this.riskDetails.Content = null;
                this.riskPenalty.Content = null;
            }
        }

        /// <summary>
        /// 查看操作历史
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link = sender as HyperlinkButton;
            EIRow row = (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            fdRecordId = row["fdKeyId"].ToString();
            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("service", "ZMCL72");
            eiInfo.Set("method", "queryOperationHistory");
            eiInfo.Set("fdRecordId", fdRecordId);
            EIBlock eIBlock = new EIBlock("result");
            eIBlock.SetRow(0, row == null ? new EIRow() : row);
            eiInfo.AddBlock(eIBlock);

            base.Submit(eiInfo, DoOperationCompleted);
        }

        public void DoOperationCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            if (inInfo.Status == -1)
            {
                string _msg = inInfo.GetMsg();
                this.OpenEFWindowAlert(_msg, true);//列表中删除，操作失败后按钮生效
                return;
            }

            OpenDetailPopup(e.EIInfo, "操作历史明细-查看", "detail", null);
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public void OpenDetailPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            OperationHistory view = new OperationHistory();//构造一个新的修改页面
            view.EIDataContext = new Platform.Core.UI.EIDataContext();
            view.EIDataContext.Update(_eIInfo);

            view.fdRecordId.Value = fdRecordId;

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面  
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = view;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(DetailWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DetailWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }
    }
}
